﻿Imports System.Windows.Controls
Imports System.Windows
Imports System.Windows.Controls.Primitives
Imports System.Windows.Data

Namespace Views

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ViewDashboardDefinition

        Sub New()

            ' Cet appel est requis par le concepteur.
            InitializeComponent()

            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub ViewDashboardDefinition_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
            If e.NewValue IsNot Nothing Then
                DefZone.Children.Clear()
                DefZone.RowDefinitions.Clear()
                DefZone.ColumnDefinitions.Clear()
                Dim def As VMDashboardDefinition = TryCast(e.NewValue, VMDashboardDefinition)
                If def Is Nothing Then
                    Dim olddef As VMDashboardDefinition = TryCast(e.OldValue, VMDashboardDefinition)
                    If olddef IsNot Nothing Then
                        RemoveHandler olddef.Zones.CollectionChanged, AddressOf ZonesCollectionChanged
                    End If
                    Return
                End If
                AddHandler def.Zones.CollectionChanged, AddressOf ZonesCollectionChanged
                If def.LayoutDefinition.ColumnCount > 1 Then
                    Dim vierge As Boolean = def.Columns.Count = 0
                    For i As Integer = 0 To def.LayoutDefinition.ColumnCount - 1
                        If i > 0 Then
                            DefZone.ColumnDefinitions.Add(New ColumnDefinition() With {.Width = New GridLength(5)})
                        End If
                        If vierge Then
                            Dim coldef = New ColumnDef()
                            Dim coldefinition = New ColumnDefinition()
                            BindingOperations.SetBinding( _
                                coldefinition, _
                                ColumnDefinition.WidthProperty,
                                New Binding("Size") With {.Source = coldef, .Mode = BindingMode.TwoWay})

                            def.Columns.Add(i, coldef)
                            DefZone.ColumnDefinitions.Add(coldefinition)
                        Else
                            Dim coldef = def.Columns(i)
                            Dim coldefinition = New ColumnDefinition()
                            BindingOperations.SetBinding( _
                                coldefinition, _
                                ColumnDefinition.WidthProperty,
                                New Binding("Size") With {.Source = coldef, .Mode = BindingMode.TwoWay})

                            DefZone.ColumnDefinitions.Add(coldefinition)
                        End If
                    Next
                    For i As Integer = 0 To def.LayoutDefinition.ColumnCount - 2
                        Dim splitter = New GridSplitter()
                        DefZone.Children.Add(splitter)
                        Grid.SetRowSpan(splitter, 2 * def.LayoutDefinition.RowCount - 1)
                        Grid.SetColumn(splitter, 1 + i * 2)
                        Grid.SetRow(splitter, 0)
                        Grid.SetColumnSpan(splitter, 1)
                    Next
                End If
                If def.LayoutDefinition.RowCount > 1 Then
                    Dim vierge As Boolean = def.Rows.Count = 0
                    For i As Integer = 0 To def.LayoutDefinition.RowCount - 1
                        If i > 0 Then
                            DefZone.RowDefinitions.Add(New RowDefinition() With {.Height = New GridLength(5)})
                        End If
                        If vierge Then
                            Dim rowdef = New RowDef()
                            Dim rowdefinition = New RowDefinition()
                            BindingOperations.SetBinding( _
                                rowdefinition, _
                                rowdefinition.HeightProperty,
                                New Binding("Size") With {.Source = rowdef, .Mode = BindingMode.TwoWay})

                            def.Rows.Add(i, rowdef)
                            DefZone.RowDefinitions.Add(rowdefinition)
                        Else
                            Dim rowdef = def.Rows(i)
                            Dim rowdefinition = New RowDefinition()
                            BindingOperations.SetBinding( _
                                rowdefinition, _
                                rowdefinition.HeightProperty,
                                New Binding("Size") With {.Source = rowdef, .Mode = BindingMode.TwoWay})

                            DefZone.RowDefinitions.Add(rowdefinition)
                        End If
                    Next
                    For i As Integer = 0 To def.LayoutDefinition.RowCount - 2
                        Dim splitter = New GridSplitter()
                        DefZone.Children.Add(splitter)
                        Grid.SetColumnSpan(splitter, 2 * def.LayoutDefinition.ColumnCount - 1)
                        Grid.SetRow(splitter, 1 + i * 2)
                        Grid.SetColumn(splitter, 0)
                        Grid.SetRowSpan(splitter, 1)
                    Next
                End If
                For Each z In def.Zones
                    Dim tg = New ToggleButton()
                    tg.Style = Resources("ZoneToggleButton")
                    DefZone.Children.Add(tg)
                    Grid.SetColumnSpan(tg, 1 + 2 * (z.ColumnCount - 1))
                    Grid.SetRow(tg, 2 * z.StartRow)
                    Grid.SetColumn(tg, 2 * z.StartColumn)
                    Grid.SetRowSpan(tg, 1 + 2 * (z.RowCount - 1))
                    tg.DataContext = z


                Next
            End If
        End Sub

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub ZonesCollectionChanged(ByVal sender As Object, ByVal e As Specialized.NotifyCollectionChangedEventArgs)
            Select Case e.Action
                Case Specialized.NotifyCollectionChangedAction.Add
                    Dim z As VMDashboardDefinitionZone = e.NewItems(0)
                    Dim tg = New ToggleButton()
                    tg.Style = Resources("ZoneToggleButton")
                    DefZone.Children.Add(tg)
                    Grid.SetColumnSpan(tg, 1 + 2 * (z.ColumnCount - 1))
                    Grid.SetRow(tg, 2 * z.StartRow)
                    Grid.SetColumn(tg, 2 * z.StartColumn)
                    Grid.SetRowSpan(tg, 1 + 2 * (z.RowCount - 1))
                    tg.DataContext = z

            End Select
        End Sub
    End Class

    Namespace Converters
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <remarks></remarks>
        Public Class DefintionToLayoutConverter
            Implements IValueConverter

            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
                Dim def = TryCast(value, VMZoneGraphDefinition)
                If def Is Nothing Then Return Nothing
                Dim param As Integer
                Try
                    param = System.Convert.ToInt32(parameter)
                Catch ex As Exception

                End Try
                Select Case param
                    Case 0
                        Return If(def.WidthAuto, Double.NaN, def.Width)
                    Case 1
                        Return If(def.HeightAuto, Double.NaN, def.Height)
                    Case 2
                        Return If(def.LeftAuto, Double.NaN, def.Left)
                    Case 3
                        Return If(def.TopAuto, Double.NaN, def.Top)
                    Case 4
                        Return If(def.RightAuto, Double.NaN, def.Right)
                    Case 5
                        Return If(def.BottomAuto, Double.NaN, def.Bottom)
                    Case 6
                        Return New Thickness( _
                            If(def.LeftAuto, 0.0, def.Left), _
                            If(def.TopAuto, 0.0, def.Top), _
                            If(def.RightAuto, 0.0, def.Right), _
                            If(def.BottomAuto, 0.0, def.Bottom))
                    Case 7
                        Return If(def.HAlignAuto, HorizontalAlignment.Stretch, [Enum].ToObject(GetType(HorizontalAlignment), def.HAlign))
                    Case 8
                        Return If(def.VAlignAuto, VerticalAlignment.Stretch, [Enum].ToObject(GetType(VerticalAlignment), def.VAlign))
                End Select

                Return Nothing
            End Function

            ''' <summary>
            ''' 
            ''' </summary>
            ''' <remarks></remarks>
            Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
                Throw New NotImplementedException()
                ' TODO
            End Function
        End Class
    End Namespace
End Namespace